home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / Other Langs / MacYacc ƒ / Sample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-05-27  |  8.8 KB  |  293 lines  |  [TEXT/EDIT]

  1.  
  2. #line 2 "Newstuff A 5/26/87:Sample.y"
  3. /* this is a test file for macyacc It should be renamed as something.y.
  4.  * MacYacc expects files with the extension '.y' or '.Y'
  5.  * it then produces a compilable file with the extension '.c'.
  6.  * If requested, it produces a '.h' include file and a '.o'
  7.  * grammar file (a list of the states, shifts, etc.)
  8.  * The '.c' file should be compiled and linked in a environment
  9.  * that simulates a standard terminal (such as linking with the
  10.  * Consular stdlib).
  11.  * The parser code lives in the data fork of mac yacc. Extract it w/ FEDIT
  12.  * or whatever if you would like to look at it or modify it.
  13.  * As an additional bonus, yacc contains a resource type: cstr which others
  14.  * may find useful.
  15.  *
  16.  * for additional information see:
  17.  *    Unix Utilities Manual   (Steve Johnson)
  18.  *    The Unix Programming Environment (Kernighan & Pike) (excellent book)
  19.  *    Intro to Compiler Construction w/ Unix (Schreiner & Friedman)
  20.  *
  21.  * this program is copyrighted by no-one.  Have fun.
  22.  */
  23. #include <stdio.h>
  24. #define DIGIT 257
  25. #define UMINUS 258
  26. #define yyclearin yychar = -1
  27. #define yyerrok yyerrflag = 0
  28. extern int yychar;
  29. extern short yyerrflag;
  30. #ifndef YYMAXDEPTH
  31. #define YYMAXDEPTH 150
  32. #endif
  33. #ifndef YYSTYPE
  34. #define YYSTYPE int
  35. #endif
  36. YYSTYPE yylval, yyval;
  37. #define YYERRCODE 256
  38.  
  39. # line 66 "Newstuff A 5/26/87:Sample.y"
  40.  
  41. yylex()
  42. {
  43.     int c;
  44.     
  45.     while((c = putchar(getchar())) == ' ');
  46.     
  47.     if( isdigit(c)) {
  48.         yylval = c - '0';
  49.         return(DIGIT);
  50.     }
  51.  else if(tolower(c) == 'q') c = 0;
  52.     return(c);
  53. }
  54. yyerror(str)
  55.   char *str;
  56. {
  57.     printf("%s\n",str);
  58.     return(0);
  59. }
  60. main()
  61. {
  62.     printf("Calculate Now(q to quit)...\n");
  63.     yyparse();
  64. }
  65. short yyexca[] ={
  66. -1, 1,
  67.     0, -1,
  68.     -2, 0,
  69.     };
  70. # define YYNPROD 15
  71. # define YYLAST 219
  72. short yyact[]={
  73.  
  74.    5,   5,   4,  18,   7,   6,   6,  10,  16,  17,
  75.    9,   2,   1,   0,  19,  20,  21,  22,  23,  15,
  76.    0,   0,   0,  24,  13,  11,  15,  12,  15,  14,
  77.    0,  13,  11,  13,  12,   0,  14,   0,  14,   0,
  78.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  79.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  80.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  81.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  82.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  83.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  84.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  85.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  86.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  87.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  88.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  89.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  90.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  91.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  92.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  93.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  94.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  95.    0,   0,   0,   0,   0,   0,   3,   8,   8 };
  96. short yypact[]={
  97.  
  98. -1000, -40,  -3,  -6, -11, -39, -39,-254,-1000,-1000,
  99. -1000, -39, -39, -39, -39, -39, -18,-1000,-1000,  -9,
  100.   -9,-1000,-1000,-1000,-1000 };
  101. short yypgo[]={
  102.  
  103.    0,  12,  11,   2,   4 };
  104. short yyr1[]={
  105.  
  106.    0,   1,   1,   1,   2,   3,   3,   3,   3,   3,
  107.    3,   3,   3,   4,   4 };
  108. short yyr2[]={
  109.  
  110.    0,   0,   3,   3,   1,   3,   3,   3,   3,   3,
  111.    3,   2,   1,   1,   2 };
  112. short yychk[]={
  113.  
  114. -1000,  -1,  -2, 256,  -3,  40,  45,  -4, 257,  13,
  115.   13,  43,  45,  42,  47,  37,  -3,  -3, 257,  -3,
  116.   -3,  -3,  -3,  -3,  41 };
  117. short yydef[]={
  118.  
  119.    1,  -2,   0,   0,   4,   0,   0,  12,  13,   2,
  120.    3,   0,   0,   0,   0,   0,   0,  11,  14,   6,
  121.    7,   8,   9,  10,   5 };
  122. #define YYFLAG -1000
  123. #define YYERROR goto yyerrlab
  124. #define YYACCEPT return(0)
  125. #define YYABORT return(1)
  126.  
  127. /*      parser for yacc output  */
  128.  
  129. int yydebug = 0; /* 1 for debugging */
  130. YYSTYPE yyv[YYMAXDEPTH]; /* where the values are stored */
  131. int yychar = -1; /* current input token number */
  132. int yynerrs = 0;  /* number of errors */
  133. short yyerrflag = 0;  /* error recovery flag */
  134.  
  135. yyparse() {
  136.  
  137.         short yys[YYMAXDEPTH];
  138.         short yyj, yym;
  139.         register YYSTYPE *yypvt;
  140.         register short yystate, *yyps, yyn;
  141.         register YYSTYPE *yypv;
  142.         register short *yyxi;
  143.  
  144.         yystate = 0;
  145.         yychar = -1;
  146.         yynerrs = 0;
  147.         yyerrflag = 0;
  148.         yyps= &yys[-1];
  149.         yypv= &yyv[-1];
  150.  
  151.  yystack:    /* put a state and value onto the stack */
  152.  
  153.         if( yydebug  ) printf( "state %d, char 0%o\n", yystate, yychar );
  154.                 if( ++yyps> &yys[YYMAXDEPTH] ) { yyerror( "yacc stack overflow" ); return(1); }
  155.                 *yyps = yystate;
  156.                 ++yypv;
  157.                 *yypv = yyval;
  158.  
  159.  yynewstate:
  160.  
  161.         yyn = yypact[yystate];
  162.  
  163.         if( yyn<= YYFLAG ) goto yydefault; /* simple state */
  164.  
  165.         if( yychar<0 ) if( (yychar=yylex())<0 ) yychar=0;
  166.         if( (yyn += yychar)<0 || yyn >= YYLAST ) goto yydefault;
  167.  
  168.         if( yychk[ yyn=yyact[ yyn ] ] == yychar ){ /* valid shift */
  169.                 yychar = -1;
  170.                 yyval = yylval;
  171.                 yystate = yyn;
  172.                 if( yyerrflag > 0 ) --yyerrflag;
  173.                 goto yystack;
  174.                 }
  175.  
  176.  yydefault:
  177.         /* default state action */
  178.  
  179.         if( (yyn=yydef[yystate]) == -2 ) {
  180.                 if( yychar<0 ) if( (yychar=yylex())<0 ) yychar = 0;
  181.                 /* look through exception table */
  182.  
  183.                 for( yyxi=yyexca; (*yyxi!= (-1)) || (yyxi[1]!=yystate) ; yyxi += 2 ) ; /* VOID */
  184.  
  185.                 while( *(yyxi+=2) >= 0 ){
  186.                         if( *yyxi == yychar ) break;
  187.                         }
  188.                 if( (yyn = yyxi[1]) < 0 ) return(0);   /* accept */
  189.                 }
  190.  
  191.         if( yyn == 0 ){ /* error */
  192.                 /* error ... attempt to resume parsing */
  193.  
  194.                 switch( yyerrflag ){
  195.  
  196.                 case 0:   /* brand new error */
  197.  
  198.                         yyerror( "syntax error" );
  199.                 yyerrlab:
  200.                         ++yynerrs;
  201.  
  202.                 case 1:
  203.                 case 2: /* incompletely recovered error ... try again */
  204.  
  205.                         yyerrflag = 3;
  206.  
  207.                         /* find a state where "error" is a legal shift action */
  208.  
  209.                         while ( yyps >= yys ) {
  210.                            yyn = yypact[*yyps] + YYERRCODE;
  211.                            if( yyn>= 0 && yyn < YYLAST && yychk[yyact[yyn]] == YYERRCODE ){
  212.                               yystate = yyact[yyn];  /* simulate a shift of "error" */
  213.                               goto yystack;
  214.                               }
  215.                            yyn = yypact[*yyps];
  216.  
  217.                            /* the current yyps has no shift onn "error", pop stack */
  218.  
  219.                            if( yydebug ) printf( "error recovery pops state %d, uncovers %d\n", *yyps, yyps[-1] );
  220.                            --yyps;
  221.                            --yypv;
  222.                            }
  223.  
  224.                         /* there is no state on the stack with an error shift ... abort */
  225.  
  226.         yyabort:
  227.                         return(1);
  228.  
  229.  
  230.                 case 3:  /* no shift yet; clobber input char */
  231.  
  232.                         if( yydebug ) printf( "error recovery discards char %d\n", yychar );
  233.  
  234.                         if( yychar == 0 ) goto yyabort; /* don't discard EOF, quit */
  235.                         yychar = -1;
  236.                         goto yynewstate;   /* try again in the same state */
  237.  
  238.                         }
  239.  
  240.                 }
  241.  
  242.         /* reduction by production yyn */
  243.  
  244.                 if( yydebug ) printf("reduce %d\n",yyn);
  245.                 yyps -= yyr2[yyn];
  246.                 yypvt = yypv;
  247.                 yypv -= yyr2[yyn];
  248.                 yyval = yypv[1];
  249.                 yym=yyn;
  250.                         /* consult goto table to find next state */
  251.                 yyn = yyr1[yyn];
  252.                 yyj = yypgo[yyn] + *yyps + 1;
  253.                 if( yyj>=YYLAST || yychk[ yystate = yyact[yyj] ] != -yyn ) yystate = yyact[yypgo[yyn]];
  254.                 switch(yym){
  255.                         
  256. case 3:
  257. #line 37 "Newstuff A 5/26/87:Sample.y"
  258. {    yyerrok;} break;
  259. case 4:
  260. #line 40 "Newstuff A 5/26/87:Sample.y"
  261. {    printf("%d\n",yypvt[-0]); } break;
  262. case 5:
  263. #line 44 "Newstuff A 5/26/87:Sample.y"
  264. {    yyval = yypvt[-1]; } break;
  265. case 6:
  266. #line 46 "Newstuff A 5/26/87:Sample.y"
  267. {    yyval = yypvt[-2] + yypvt[-0]; } break;
  268. case 7:
  269. #line 48 "Newstuff A 5/26/87:Sample.y"
  270. {    yyval = yypvt[-2] - yypvt[-0]; } break;
  271. case 8:
  272. #line 50 "Newstuff A 5/26/87:Sample.y"
  273. {    yyval = yypvt[-2] * yypvt[-0]; } break;
  274. case 9:
  275. #line 52 "Newstuff A 5/26/87:Sample.y"
  276. {    yyval = yypvt[-2] / yypvt[-0]; } break;
  277. case 10:
  278. #line 54 "Newstuff A 5/26/87:Sample.y"
  279. {    yyval = yypvt[-2] % yypvt[-0]; } break;
  280. case 11:
  281. #line 56 "Newstuff A 5/26/87:Sample.y"
  282. {     yyval = - yypvt[-0]; } break;
  283. case 13:
  284. #line 61 "Newstuff A 5/26/87:Sample.y"
  285. { yyval = yypvt[-0];} break;
  286. case 14:
  287. #line 63 "Newstuff A 5/26/87:Sample.y"
  288. {    yyval = 10 *yypvt[-1] + yypvt[-0]; } break;
  289.                 }
  290.                 goto yystack;  /* stack new state and value */
  291.  
  292.         }
  293.